package com.ms.qw.jsf.converter;

import com.ms.qw.application.cvl.CvlQueryService;
import com.ms.qw.domain.support.*;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;
import java.util.Map;

/**
 * User: aglwkrs
 * Date: 30/11/11
 */

@FacesConverter("com.ms.qw.jsf.converter.CvlGenericConverter")
public class CvlGenericConverter implements Converter {

    private String categoria;

    public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
        return null;
    }

    public String getAsString(FacesContext context, UIComponent component, Object value) throws ConverterException {
        WebApplicationContext requiredWebApplicationContext = FacesContextUtils.getRequiredWebApplicationContext(context);
        CvlQueryService CvlQueryService = (CvlQueryService) requiredWebApplicationContext.getBean("cvlQueryServiceMyBatisImpl");
        Map cvls = CvlQueryService.getCategoria(categoria);
        Describible desc=null;
        if("tipoCampana".equals(categoria)|| "categoriaCampana".equals(categoria)){
            desc = (Describible) cvls.get(String.valueOf(new Integer((String)value)));
        }else{
            desc = (Describible) cvls.get(String.valueOf(value));
        }
        if (desc == null) {
            return "<No Encontrado>";
        }
        return desc.getDescripcion();
    }




    public String getCategoria() {
        return categoria;
    }

    public void setCategoria(String categoria) {
        this.categoria = categoria;
    }

}
